<template>
  <div>
    <BasicModal :height="400"
                v-bind="$attrs"
                @register="registerModal"
                :title="getTitle"
                @ok="handleSubmit">
      <BasicForm @register="registerForm" />
    </BasicModal>
  </div>
</template>
<script lang="ts">
import { defineComponent, ref, computed, unref, h } from 'vue';
import { BasicForm, useForm } from '/@/components/Form/index';
import { BasicModal, useModalInner } from '/@/components/Modal';
// import { listApi } from '/@/api/set/menu';
import { EditApi } from '/@/api/activityAdmin/index';
import { useComponentRegister } from '/@/components/Form';
import { listToTree } from '/@/utils/helper/treeHelper';
import { FormSchema } from '/@/components/Table';
import { Tinymce } from '/@/components/Tinymce/index';
import { getAppEnvConfig } from '/@/utils/env';

import { UserItem } from '/@/api/user/userModel';
useComponentRegister('Tinymce', Tinymce);
export const formSchema: FormSchema[] = [
  {
    label: 'id',
    field: 'id',
    component: 'Input',
    show: false,
  },
  {
    field: 'returnRemark',
    label: '驳回原因:',
    component: 'InputTextArea',
    colProps: { span: 22 },
    rules: [
      {
        required: true,
        message: '请输入驳回原因',
      },
    ],
  },
];

export default defineComponent({
  name: 'MenuModal',
  components: { BasicModal, BasicForm },
  emits: ['successModel', 'register'],
  setup(_, { emit }) {
    const isUpdate = ref(true);
    const Delete = ref({});
    const [registerForm, { resetFields, setFieldsValue, updateSchema, validate }] = useForm({
      schemas: formSchema,
      labelWidth: 100,
      showActionButtonGroup: false,
      actionColOptions: {
        span: 23,
      },
    });

    const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
      console.log(data.record, '编辑内容', getAppEnvConfig().VITE_GLOB_API_URL);
      Delete.value = data.record;
      resetFields();
      isUpdate.value = !!data?.isUpdate;
      if (data.record.id) {
        setFieldsValue({
          ...data.record,
        });
      }
      // const treeData = await queryList();
      // updateSchema({
      //   field: 'parentId',
      //   componentProps: {
      //     treeData,
      //   },
      // });
    });

    const getTitle = '驳回原因';

    async function handleSubmit() {
      try {
        setModalProps({ confirmLoading: true });
        const values = await validate();
        console.log(values, '验证');
        const data = { ...Delete.value, ...values };
        data.isUse = 3;
        console.log(data, '验证1');
        await EditApi(data);
        closeModal();
        emit('successModel'); //返回
      } catch (err) {
        console.log(err);
      } finally {
        setModalProps({ confirmLoading: false });
      }
    }

    return { registerModal, registerForm, getTitle, handleSubmit };
  },
});
</script>
